package com.gs;

import java.sql.SQLException;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.gs.business.AltreUtil;
import com.gs.business.DBUtil;
import com.gs.business.Database;
import com.gs.view.entrate.EntrateView;
import com.gs.view.login.Login;
import com.gs.view.login.Registrazione;
import com.gs.view.uscite.UsciteView;
import com.molinari.jdbcandroid.wrap.ConnectionAndroid;
import comi.gsi.ascoltatori.ListenerClickApriActivity;
import comi.gsi.view.MyBaseActivity;

public class GSAActivity extends MyBaseActivity {

	private TextView salu = null;
	private Button btnEntrate = null;
	private Button btnUscita = null; 
	private TextView totUscite = null;
	private TextView mediaUscite = null;
	private TextView totEntrate = null;
	private TextView mediaEntrate = null;
	private static Controllore controller = Controllore.getSingleton();

	@Override
	protected int getIdLayout() {
		return R.layout.main;
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		final TextView totUscite = (TextView) findViewById(R.id.euroTotU);
		if(totUscite!= null){
			final Double tUscite = AltreUtil.arrotondaDecimaliDouble(Database.Annuale());
			totUscite.setText("Euro " + Double.toString(tUscite));
		}
	}

	@Override
	protected void myOnCreate(final Bundle savedInstanceState) {

		DBUtil.connection = new ConnectionAndroid(this, true);
		DBUtil.applicationContext = getApplicationContext();
		try {
			Controllore.setStartCategorie();
		}
		catch (SQLException e) {
		}
		catch (InstantiationException e) {
			e.printStackTrace();
		}
		catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		final Button btnLogin = (Button) findViewById(R.id.Login);
		final Button btnRegister = (Button) findViewById(R.id.btnRegister);

		if (Controllore.getUtenteLogin() == null) {
			btnLogin.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(final View v) {
					final Intent i = new Intent(v.getContext(), Login.class);
					v.getContext().startActivity(i);
				}
			});

			btnRegister.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(final View v) {
					final Intent i = new Intent(v.getContext(), Registrazione.class);
					v.getContext().startActivity(i);
				}
			});
		} else {
			setContentView(R.layout.mainloggato);
			//myRemoveView(new View[] { btnLogin, btnRegister, labelLogin, labelRegister });
			
			salu = (TextView) lazyLoad(salu, R.id.saluto1);
			salu.setText(getMessaggio("ciao", new String[] { Controllore.getUtenteLogin().getNomeEntita() }));

			btnEntrate = (Button) lazyLoad(btnEntrate,R.id.addEntry);
			btnEntrate.setVisibility(Button.VISIBLE);
			btnEntrate.setOnClickListener(new ListenerClickApriActivity(EntrateView.class));

			btnUscita = (Button) lazyLoad(btnUscita,R.id.addCharge);
			btnUscita.setVisibility(Button.VISIBLE);
			btnUscita.setOnClickListener(new ListenerClickApriActivity(UsciteView.class));

			final Double tUscite = AltreUtil.arrotondaDecimaliDouble(Database.Annuale());
			totUscite = (TextView) lazyLoad(totUscite, R.id.euroTotU);
			totUscite.setText("Euro " + Double.toString(tUscite));

			final Double tEntrate = AltreUtil.arrotondaDecimaliDouble(Database.EAnnuale());
			totEntrate = (TextView) lazyLoad(totUscite, R.id.euroTotE);
			totEntrate.setText("Euro " + Double.toString(tEntrate));

			final Double mUscite = AltreUtil.arrotondaDecimaliDouble(Database.MensileInCorso());
			mediaUscite = (TextView) lazyLoad(mediaUscite, R.id.euroMediaU);
			mediaUscite.setText("Euro " + Double.toString(mUscite));

			final Double mEntrate = AltreUtil.arrotondaDecimaliDouble(Database.EMensileInCorso());
			mediaEntrate = (TextView) lazyLoad(mediaEntrate, R.id.euroMediaE);
			mediaEntrate.setText("Euro " + Double.toString(mEntrate));

		}

	}

	@Override
	protected Dialog onCreateDialog(final int id) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		if (id == 0) {
			builder.setTitle("Avviso");
			builder.setMessage("Attenzione! Questo e' un avviso!");
			builder.setCancelable(true);
		} else if (id == 1) {
			builder.setTitle("Conferma");
			builder.setMessage("Vuoi proseguire con l'operazione?");
			builder.setCancelable(false);
			builder.setPositiveButton("OK?", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(final DialogInterface dialog, final int which) {

				}
			});
		} else if (id == 2) {
			builder.setTitle("Conferma");
			builder.setMessage("Vuoi annullare l'operazione?");
			builder.setCancelable(false);
			builder.setNegativeButton("OK?", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(final DialogInterface dialog, final int which) {
					// TODO Auto-generated method stub

				}
			});

		} else if (id == 3) {
			final CharSequence[] items = { "Caffe'", "Gelato", "Te'", "Birra", "Ragu'" };
			final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getApplicationContext(), android.R.layout.simple_spinner_item, items);
			final Spinner spinner = new Spinner(getApplicationContext());
			spinner.setAdapter(adapter);
			builder.setTitle("Scelta multipla");
			builder.setMessage("Vuoi proseguire con l'operazione?");
			builder.setItems(items, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(final DialogInterface dialog, final int which) {
					Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
				}

			});

		}
		return builder.create();
	}

	public static int getAnno() {
		return Controllore.getAnno();
	}

	public static void setAnno(final int anno) {
		Controllore.setAnno(anno);
	}
}